package javabase.schoolwork.day6_10;

import java.util.Random;

/**
 * @author fance
 * @date 2018/6/10 15:31
 */
class Employer implements  Runnable {
    private static int count = 100;
    private static int back = 0;
    private static int front = 0;

    @Override
    public void run() {
        while (count > 1) {
            synchronized (this) {
                if("前门".equals(Thread.currentThread().getName())){
                    front++;
                }else if("后门".equals(Thread.currentThread().getName())){
                    back++;
                }

                System.out.println("第"+count--+"名员工，从"+//
                        Thread.currentThread().getName()+"出，取出的双色球的号码为："+Company.getNumbers());
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public static int getCount() {
        return count;
    }

    public static void setCount(int count) {
        Employer.count = count;
    }

    public static int getBack() {
        return back;
    }

    public static void setBack(int back) {
        Employer.back = back;
    }

    public static int getFront() {
        return front;
    }

    public static void setFront(int front) {
        Employer.front = front;
    }
}
public class Company {
    private static Random random = new Random(47);

    public static void main(String[] args) throws InterruptedException {
            Employer employer = new Employer();
            new Thread(employer,"前门").start();
            new Thread(employer,"后门").start();
            Thread.sleep(5000);
        System.out.println("前门总计:"+Employer.getFront()+"人\t后门总计:"+Employer.getBack()+"人\t总计："+(100 -Employer.getCount())+"人");
    }



    /**
     * 生成彩票号码
     * @return
     */
    public static String getNumbers() {
        StringBuilder stringBuilder = new StringBuilder("[");
        for (int i = 0; i < 7; i++) {
            int cur = random.nextInt(100);
            if (cur < 10) {
                stringBuilder.append("0").append(cur).append(",");
             } else {
                stringBuilder.append(cur).append(",");
            }

        }
        stringBuilder.setCharAt(stringBuilder.length() - 1,']');
        return stringBuilder.toString();
    }
}
